package com.example.sqrt;

// 牛顿迭代 (n/x + x) / 2 -> n/x == n
public class Newton {

    // n是一个假想值，任意取值
    private static double newton(double n,int x) {
        if (x == 0) {
            return 0;
        }
        return sqrt(n, x);
    }

    private static double sqrt(double i, int x) {
        double res = (x/i + i) / 2;
        if (res == i) {
            return i;
        } else {
            return sqrt(res, x);
        }
    }

    public static void main(String[] args) {
        System.out.println(newton(3, 5));
    }

}
